package com.eight.cloud.message.handler;

import com.eight.cloud.redisson.utils.TokenManager;
import org.jetbrains.annotations.NotNull;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.DefaultHandshakeHandler;

import javax.annotation.Resource;
import java.security.Principal;
import java.util.Map;

import static com.eight.cloud.core.global.constant.StrConstant.*;

/**
 * 自定义websocket握手处理
 *
 * @ClassName: WebsocketHandshakeHandler
 * @Author: TXC
 * @Date: 2024-10-10 16:39
 **/
@Component
public class WebsocketHandshakeHandler extends DefaultHandshakeHandler {
    @Resource
    private TokenManager tokenManager;

    /**
     * 确定用户  用户id为websocket session唯一标识
     *
     * @return
     */
    @Override
    protected Principal determineUser(ServerHttpRequest request, WebSocketHandler wsHandler,
                                      @NotNull Map<String, Object> attributes) {
        return () -> attributes.get(USER_TYPE).toString() + DASH + attributes.get(USER_ID).toString();
    }
}
